package com.woniu.car.controller;

import com.woniu.car.common.BeanCopyUtils;
import com.woniu.car.common.ResponseEntity;
import com.woniu.car.domain.*;
import com.woniu.car.dto.*;
import com.woniu.car.exception.AccountException;
import com.woniu.car.exception.ParamException;
import com.woniu.car.param.*;
import com.woniu.car.service.*;
import com.woniu.car.util.Code;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName RolePermissionController
 * @Description
 * @Author 王维鑫
 * @Version
 **/
@Api(tags = "角色权限控制器")
@RestController
@RequestMapping("/rolePermission")
public class RolePermissionController {
    @Resource
    private TRoleService tRoleService;
    @Resource
    private TPermissionService tPermissionService;
    @Resource
    private TRolePermissionService tRolePermissionService;
    @Resource
    private TRoleUserService tRoleUserService;
    @Resource
    private TUrlService tUrlService;
    @Resource
    private TUserAccountService tUserAccountService;
    @Resource
    private TBackstageAccountService tBackstageAccountService;
    /**
     *@Author 王维鑫
     *@Description 新增角色
     *@Param
     *@return
     **/
    @ApiOperation("新增角色")
    @PostMapping("insertRole")
   // @BackstagePermissionCheck
    public ResponseEntity insertRole(HttpServletRequest request,RoleParam param) throws ParamException {
        tRoleService.insertRole(param);
        return new ResponseEntity().setCode(Code.NORMAL).setMsg("角色新增成功");
    }

    /**
     *@Author 王维鑫
     *@Description 查看所有角色
     *@Param
     *@return
     **/
        @ApiOperation("查看所有角色")
        @GetMapping("showAllRole")
       // @BackstagePermissionCheck
       public ResponseEntity showAllRole(HttpServletRequest request){
           List<TRole> tRoles = tRoleService.showRole();
           List<RoleDto> roleDtos = BeanCopyUtils.copyList(tRoles, RoleDto::new);
           return ResponseEntity.builderList(RoleDto.class).setObject(roleDtos).setCode(Code.NORMAL).setMsg("查到所有角色");
       }

    /**
     *@Author 王维鑫
     *@Description 用户角色授权
     *@Param
     *@return
     **/
    @ApiOperation("用户角色授权")
    @PostMapping("userRole")
    //@BackstagePermissionCheck
    public ResponseEntity userRole(HttpServletRequest request,UserRoleParam param) throws Exception {
        TRoleUser tRoleUser = new TRoleUser();
        Integer userId = param.getUserId();
        Integer roleId = param.getRoleId();
        //判断用户存不存在
        TBackstageAccount tBackstageAccount = new TBackstageAccount();
        tBackstageAccount.setBackstageId(userId);
        List<TBackstageAccount> tBackstageAccounts = tBackstageAccountService.queryBackstageAccount(tBackstageAccount);
        if(tBackstageAccounts.size()==0){
            throw new AccountException("该授权用户不存在");
        }else {
            //判断角色存不存在
            TRole tRole = new TRole();
            tRole.setRoleId(roleId);
            List<TRole> tRoles = tRoleService.queryRole(tRole);
            System.out.println("查到："+tRoles.size());
            if(tRoles.size()==0){
                throw new AccountException("该授权角色不存在");
            }else {
                tRoleUser.setRoleId(roleId);
                List<TRoleUser> tRoleUsers = tRoleUserService.queryRoleUser(tRoleUser);
                for (TRoleUser roleUser : tRoleUsers) {
                    Integer userId1 = roleUser.getUserId();
                    if(userId1.equals(userId)){
                        throw new ParamException("该用户已经拥有该角色");
                    }
                }
                tRoleUser.setUserId(userId);
                tRoleUserService.insertRoleUser(tRoleUser);
                return new ResponseEntity().setMsg("角色授权成功").setCode(Code.NORMAL);
            }
        }
    }

//    /**
//     *@Author 王维鑫
//     *@Description 查看后台功能接口
//     *@Param
//     *@return
//     **/
//    @ApiOperation("查看所有功能")
//    @GetMapping("showFunction")
//   // @BackstagePermissionCheck
//    public ResponseEntity showFunction(HttpServletRequest request){
//        List<TUrl> tUrls = tUrlService.showAllUrls();
//        List<UrlDto> objects = BeanCopyUtils.copyList(tUrls, UrlDto::new);
//        return ResponseEntity.builderList(UrlDto.class).setObject(objects).setMsg("查到所有功能").setCode(Code.NORMAL);
//    }
    @ApiOperation("查看所有功能")
    @GetMapping("showFunction")
    // @BackstagePermissionCheck
    public ResponseEntity showFunction(HttpServletRequest request,PageParam param){
        PageDto pageDto = tUrlService.showAllUrlss(param);
        return ResponseEntity.builder(PageDto.class).setObject(pageDto).setMsg("查到所有功能").setCode(Code.NORMAL);
    }
    /**
     *@Author 王维鑫
     *@Description 角色权限分配
     *@Param
     *@return
     **/
    @ApiOperation("角色权限分配")
    @PostMapping("roleAuthorize")
   // @BackstagePermissionCheck
    public ResponseEntity roleAuthorize(HttpServletRequest request, RoleAuthorizeParam param) throws Exception {
//        String token = request.getHeader("token");
//        System.out.println("token:"+token);
//        String id = JWTUtil.getId(token);
//        Integer i=Integer.getInteger(id);
        tUrlService.roleAuthorize(param);
        return new ResponseEntity().setCode(Code.NORMAL).setMsg("授权成功");
    }



    /**
     *@Author 王维鑫
     *@Description 根据用户ID查询权限
     *@Param
     *@return
     **/
    @ApiOperation("根据用户ID查询权限")
    @GetMapping("queryPermissionById")
   // @BackstagePermissionCheck
    public ResponseEntity queryPermissionById(HttpServletRequest request,BackstageQueryPermissionParam param) throws ParamException {
        List<PermissionsDto> permissionsDtos = tRolePermissionService.queryPermissionById(param);
        return ResponseEntity.builderList(PermissionsDto.class).setObject(permissionsDtos).setMsg("查询成功");
    }

    /**
     *@Author 王维鑫
     *@Description 查看用户拥有角色
     *@Param
     *@return
     **/
    @ApiOperation("查看用户拥有角色")
    @GetMapping("queryUserPermission")
   // @BackstagePermissionCheck
    public ResponseEntity queryUserPermission(HttpServletRequest request,BackstageQueryPermissionParam param){
        List<RoleDto> roleDtos = tRoleUserService.queryUserPermission(param);
        return ResponseEntity.builderList(RoleDto.class).setObject(roleDtos).setMsg("查询成功").setCode(Code.NORMAL);
    }
    @ApiOperation("查看所有后台用户")
    @GetMapping("queryAllBackstage")
   // @BackstagePermissionCheck
    public ResponseEntity queryAllBackstage(HttpServletRequest request){
        List<AllBackstageDto> allBackstageDtos = tUrlService.queryAllBackStage();
        return ResponseEntity.builderList(AllBackstageDto.class).setObject(allBackstageDtos).setMsg("查询成功").setCode(Code.NORMAL);
    }


}
